#! /usr/bin/env python
# coding=utf-8
# 使用cut adaptor 对 reads 进行剪切
tmp_file = r'cut_adaptor_tmp'




import time
import os
import shutil
import argparse
import sys






parser = argparse.ArgumentParser(
    description=''' 使用cutadapt 去除 barcode 序列的引物 
    这个程序在barcode_assign_to_file.py运行完成后 对result 运行
    用法:
    run_cut_adaptor_trim.py  -i result -l ATTCAACCAATCATAAAGATATTGG -r TGATTTTTTGGACATCCAGAAGTTTA -o cutprimer
    由大天才于2022年2月17日创建于浙江农业大学''')

parser.add_argument('-i',
                help='run_cut_adaptor_trim.py 输出的存放着各个标本barcode fastq的 文件')

parser.add_argument('-l',
                help='5p引物序列')
parser.add_argument('-r',
                help='3p引物序列')

parser.add_argument('-o',
                help='输出的目录')

args = parser.parse_args()


if not args.i or not args.o or not args.l or not args.r:
    parser.print_help()
    sys.exit()




infile = args.i

outfile = args.o

left_primer = args.l #r'ATTCAACCAATCATAAAGATATTGG'

right_primer = args.r #r'TGATTTTTTGGACATCCAGAAGTTTA'

try:
	shutil.rmtree(tmp_file)
except:
	pass
try:
	os.mkdir(tmp_file)
except:
	pass

try:
	shutil.rmtree(outfile)
except:
	pass
try:
	os.mkdir(outfile)
except:
	pass



for i in os.listdir(infile):
	# 运行 cutadaptor
	a = os.system('cutadapt -g %s  --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+i, infile +'/'+i))
	#print('cutadapt -g %s  --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+i, infile +'/'+i))
	
	b = os.system('cutadapt -a %s  --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+i, tmp_file+'/'+i))
	#print('cutadapt -a %s  --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+i, tmp_file+'/'+i))
	